 var currentObj;
 function EditRole(roleId,obj){
    currentObj = obj;
	// Ajax get Role  Info
	/*-------------function begin---------------------------*/	
	$.ajax({
			type: "post",
	    	dataType:"xml",
			"url" : "AjaxGetRoleServlet",
			//提交的数�
			data: {_roleId:roleId},
			beforeSend: function(XMLHttpRequest){
				
            },
            success: function(data, textStatus){
                 var result    =  $(data).find("result").text();
                 if( result == "true"){
                 	LoadRoleInfo(data);
                 	showEditRole();
                 }
		    },
			complete: function(XMLHttpRequest, textStatus){
			},
			error: function(){
				
			}
	})
	/*-------------function end  ---------------------------*/
 }
 function LoadRoleInfo(xmlData){
 	var roleId   = $(xmlData).find("roleId").text();
 	var title    = $(xmlData).find("title").text();
 	var identify = $(xmlData).find("identify").text();
 	var comment  = $(xmlData).find("comment").text();
 	document.getElementById("E_roleId").value   = roleId;
 	document.getElementById("E_title").value    = title;
 	document.getElementById("E_identify").value = identify;
 	document.getElementById("E_comment").value  = comment;
 	
 }
 
 function showEditRole(){
 	var screenwidth, screenheight, mytop, getPosLeft, getPosTop          
	screenwidth     = $(window).width();          
	screenheight    = $(window).height();                    //获取滚动条距顶部的偏�   
	mytop           = $(document).scrollTop();               //计算弹出层的left  
	var myheight    = $("#RoleEditContainer").height();
	var mywidth     = $("#RoleEditContainer").width(); 
	getPosLeft      = (screenwidth - mywidth)/2;             //计算弹出层的top        
	getPosTop       = (screenheight - myheight)/2 -30; 
	$("#RoleEditContainer").css({ "left": getPosLeft, "top": getPosTop });
	
	showCover()
	$("#RoleEditContainer").css("display", "block");
	$("#EditRole_close").click( function(){  
	     $("#RoleEditContainer").css({"display" : "none"});
	     hideCover();
	});
 }
 function hidenEditRole(){
 	document.getElementById("E_roleId").value   = "";
 	document.getElementById("E_title").value    = "";
 	document.getElementById("E_identify").value = "";
 	document.getElementById("E_comment").value  = "";
 	 $("#RoleEditContainer").css({"display" : "none"});
     hideCover();
 }
 
 function submitEditRole(){
    $(":input").css("border-color","");
     if($("#E_title").val().trim().length == 0){
        $("#E_title").css("border-color","red");
        alert("Title 不能为空！");
        return;
     }
     if($("#E_identify").val().trim().length == 0){
     	$("#E_identify").css("border-color","red");
        alert("Identify 不能为空！");
        return;
     }
 	// Ajax Modify Role info
	/*-------------function begin---------------------------*/	
	var roleId   = document.getElementById("E_roleId").value;
 	var title    = document.getElementById("E_title").value ;
 	var identify = document.getElementById("E_identify").value;
 	var comment  = document.getElementById("E_comment").value;
 	//alert(roleId+","+title+","+identify+","+comment);
	$.ajax({
			type: "post",
	    	dataType:"xml",
			"url" : "AjaxModifyRoleInfoServlet",
			//提交的数�
			data: {_roleId:roleId,_title:title,_identify:identify,_comment:comment},
			beforeSend: function(XMLHttpRequest){
            },
            success: function(data, textStatus){
                 var result    =  $(data).find("result").text();
                 if( result == "true"){
                 	var obj = $(currentObj).parent().parent();  
                 	$((obj.children())[0]).html(title);
                 	$("#opNoticeSpan").html("操作成功！");
   		 	 		setTimeout(function(){$("#opNoticeSpan").html("")},3000);
                 }else{
                 	fileNotice();
                 }
                  hidenEditRole();
		    },
			complete: function(XMLHttpRequest, textStatus){
			},
			error: function(){
				
			}
	})
	/*-------------function end  ---------------------------*/
 }
 function fileNotice(){
 	$("#opNoticeSpan").html("操作失败，存在同名的角色（或者Identify相同）.");
   	setTimeout(function(){$("#opNoticeSpan").html("")},3000);
 }
 function submitAddRole(){
 	// Ajax ADD Role info
	/*-------------function begin---------------------------*/	
 	var title    = document.getElementById("_title").value ;
 	var identify = document.getElementById("_identify").value;
 	var comment  = document.getElementById("_comment").value;
 	
	$.ajax({
			type: "post",
	    	dataType:"xml",
			"url" : "AjaxModifyRoleInfoServlet",
			//提交的数�
			data: {_roleId:0,_title:title,_identify:identify,_comment:comment},
			beforeSend: function(XMLHttpRequest){
            },
            success: function(data, textStatus){
                 var result    =  $(data).find("result").text();
                 var roleId = $(data).find("roleId").text();
                 if( result == "true"){
                 	addRoleContainer(data);
                 	cleanAddContanter();
                 	$("#opNoticeSpan").html("操作成功！");
   		 	 		setTimeout(function(){$("#opNoticeSpan").html("")},3000);
                 }else{
                 	 fileNotice();
                 }
		    },
			complete: function(XMLHttpRequest, textStatus){
			},
			error: function(){
				
			}
	})
	/*-------------function end  ---------------------------*/
 }
 function addRoleContainer(xmlData){
    var roleId = $(xmlData).find("roleId").text();
    var title  = $(xmlData).find("title").text();
    $("#ListDiv_cleanfloat").remove();
 	$("#QAmembersListDiv").append(
 	"<div style='float:left;width:200px; margin:5px;padding-top:5px; border:1px dotted #CCC;'>"
       +"<div style='float:left;width:125px;'>"+title+"</div>"
       +"<div style='float:left;width:25px;cursor:pointer;'><img onclick='AssignOpToRole("+roleId+",\""+title+"\")' class='buttonBcImg' width='20' src='./images/role_set.png' /></div>"
       +"<div style='float:left;width:25px;cursor:pointer;'><img onclick='EditRole("+roleId+",this)' class='buttonBcImg' width='20' src='./images/edit.jpg' /></div>"
       +"<div style='float:left;width:25px;cursor:pointer;'><img onclick='DeleteRole("+roleId+",this)' class='buttonBcImg' width='20' src='./images/delete.png' /></div>"
       +"<div style='clear:both' ></div>"
  	+"</div><div id='ListDiv_cleanfloat' style='clear:both' ></div>"
 	);
 }
 
 function cleanAddContanter(){
 	document.getElementById("_title").value = "";
 	document.getElementById("_identify").value = "";
 	document.getElementById("_comment").value  = "";
 }

 
 function DeleteRole(roleId,obj){
	 if(!confirm("确定要删除该角色吗?")){
	 	return 
	 } 
 	// Ajax get Role  Info
	/*-------------function begin---------------------------*/	
	$.ajax({
			type: "post",
	    	dataType:"xml",
			"url" : "AjaxDeletetRoleServlet",
			//提交的数�
			data: {_roleId:roleId},
			beforeSend: function(XMLHttpRequest){
            },
            success: function(data, textStatus){
                 var result    =  $(data).find("result").text();
                 if( result == "true"){
                 	$(obj).parent().parent().remove();
                 }
		    },
			complete: function(XMLHttpRequest, textStatus){
			},
			error: function(){
				
			}
	})
	/*-------------function end  ---------------------------*/
 }
 function AssignOpToRole(roleId,role_title){
    document.getElementById("assign_roleId").value = roleId;
 	$("#AssignOp_title").html("为[<span style='color:red;'>"+role_title+"</span>]角色 指派操作");
 	ajaxGetOperations(roleId);
 	showContainer("AssignOpToRoleDiv","AssignOp_close");
 }
 function hidenAssignOp(){
 	hidenContainer("AssignOpToRoleDiv");
 }
 function ajaxGetOperations(roleId){
 	// Ajax GET  QA Members 
	/*-------------function begin---------------------------*/	
	$.ajax({
			type: "post",
	    	dataType:"xml",
			"url" : "AjaxGetRoleOperationsServlet",
			//提交的数�
			data: {roleId:roleId},
			beforeSend: function(XMLHttpRequest){
				$("#OperationListDiv").html("数据加载中……");
            },
            success: function(data, textStatus){
               loadOperations(data);
		    },
			complete: function(XMLHttpRequest, textStatus){
			},
			error: function(){
				
			}
	})
	/*-------------function end  ---------------------------*/
 }
 
 function loadOperations(xmlData){
 	
	$("#OperationListDiv").html("");
	 var title       = "";
     var operationId = "";
     var isContains  = "";
     $(xmlData).find("OperationItem").each(function(index, element) {
        title       = $(this).children("title").text();
        operationId = $(this).children("operationId").text();
        isContains  = $(this).children("isContains").text();
        if(isContains == "yes"){
        	$("#OperationListDiv").append("<div style='float:left;width:180px;'><input type='checkbox' name='assign_operationId' checked='checked' value='"+operationId+"' />"+title+"</div>");
        }else{
        	$("#OperationListDiv").append("<div style='float:left;width:180px;'><input type='checkbox' name='assign_operationId' value='"+operationId+"' />"+title+"</div>");
        }
     });
     $("#OperationListDiv").append("<div style='clear:both' ></div>");
 }
 
 
 
 function get_assign_opIds(){
 	assign_opIds = document.getElementsByName("assign_operationId");
 	var choose_Ids="";
 	for(i = 0; i < assign_opIds.length ; i++){
 		if(assign_opIds[i].checked){
 			choose_Ids =choose_Ids + assign_opIds[i].value + ",";
 		}
 	}
 	if(choose_Ids.length >0){
 		choose_Ids = choose_Ids.substr(0,choose_Ids.length-1);
 	}
 	
 	return choose_Ids;
 }
 
 function submitAssignOpsToRole(){
 	var choose_opIds = get_assign_opIds();
 	var roleId       = document.getElementById("assign_roleId").value;
 	//alert(choose_opIds);
 	// Ajax GET  QA Members 
	/*-------------function begin---------------------------*/	
	$.ajax({
			type: "post",
	    	dataType:"xml",
			"url" : "SubmitAssignOpsToRoleServlet",
			//提交的数�
			data: {roleId:roleId,operationIds:choose_opIds},
			beforeSend: function(XMLHttpRequest){
            },
            success: function(data, textStatus){
               var result    =  $(data).find("Result").text();
               if(result == "success"){
                 	$("#opNoticeSpan").html("【角色设置】操作成功！");
   		 	 		setTimeout(function(){$("#opNoticeSpan").html("")},3000);
               }
               hidenAssignOp();
		    },
			complete: function(XMLHttpRequest, textStatus){
			},
			error: function(){
				
			}
	})
	/*-------------function end  ---------------------------*/
 	
 }
 